﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdaptiveDeNet.RAND;

namespace AdaptiveDeNet.Functions
{
    public class Ackley : Function
    {
        public double[] o;

        public Ackley(double[] upperBound, double[] lowerBound)
        {
            o = new double[upperBound.Length];

            for (int i = 0; i < o.Length; i++)
            {
                o[i] = StaticRand.NextDouble() * (upperBound[i] - lowerBound[i]) + lowerBound[i];
            }
        }
        #region Function Members

        public double Feval(double[] x)
        {

            double fx = 0;
            
            double sum1 = 0;
            double sum2 = 0;
            double D = 30;

            for (int i = 0; i < 30; i++)
            {
                sum1 += (x[i] - o[i]) * (x[i] - o[i]);    
            }

            for (int i = 0; i < 30; i++)
            {
                sum2 += Math.Cos(2 * Math.PI * (x[i] - o[i]));    
            }

            fx = -20 * Math.Exp(-0.02 * Math.Sqrt(sum1 / D)) - Math.Exp(sum2 / D) + 20 + Math.Exp(1);

            return fx;
        }

        #endregion

    }
}
